﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;

namespace Shop.Content.upload.fiels
{



    public class up : IHttpHandler
    {

        private const int UploadFileLimit = 3;//上传文件数量限制

        private string _msg = "上传成功！";//返回信息

        public void ProcessRequest(HttpContext context)
        {
            StringBuilder filepathnow = new StringBuilder();
            string user = context.Request.UrlReferrer.ToString();
            int iTotal = context.Request.Files.Count;

            if (iTotal == 0)
            {
                _msg = "没有数据";
            }
            else
            {
                int iCount = 0;

                for (int i = 0; i < iTotal; i++)
                {
                    HttpPostedFile file = context.Request.Files[i];
                    StringBuilder str_filename = new StringBuilder();
                    string time = DateTime.Now.ToString();
                    time = time.Replace("-", "").Replace(":", "").Replace(" ", "");
                    Random rand = new Random();
                    string tempstr = rand.Next(10000, 99999).ToString(); ;
                    string tempfilename = file.FileName;
                    string[] tempstrs = tempfilename.Split(new char[] { '.' });
                    string filetype = tempstrs[tempstrs.Length-1].ToString();
                    filetype = filetype.ToLower();
                    string filetypenow = string.Empty;
                    switch (filetype)
                    {
                        case "jpg":
                            filetypenow = "jpg";
                            break;
                        case "jpeg":
                            filetypenow = "jpeg";
                            break;
                        case "gif":
                            filetypenow = "gif";
                            break;
                        case "png":
                            filetypenow = "png";
                            break;
                        default:
                            filetypenow = "jpg";
                            break;

                    }
                    str_filename.Append(time);
                    str_filename.Append(tempstr);
                    str_filename.Append(".");
                    str_filename.Append(filetypenow);
                    
                    if (file.ContentLength > 0 || !string.IsNullOrEmpty(file.FileName))
                    {
                        //保存文件
                        file.SaveAs(System.Web.HttpContext.Current.Server.MapPath("fiels/" + Path.GetFileName(str_filename.ToString())));
                        //安全设置1只允许上传gif和jpeg图片jgp,png
                        //这里可以根据实际设置其他限制
                        filepathnow.Append(",");
                        filepathnow.Append("/content/upload/fiels/");
                        filepathnow.Append(str_filename.ToString());
                        if (++iCount > UploadFileLimit)
                        {
                            _msg = "超过上传限制：" + UploadFileLimit;
                            break;
                        }
                    }
                }
            }
            context.Response.Write("<script>window.parent.Finish('" + _msg + "','"+filepathnow.ToString()+"');</script>");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
